#include "connectiondialog.h"

ConnectionDialog::ConnectionDialog(QWidget * parent)
    : QDialog(parent)
{
setupUi(this);

app_dir = QCoreApplication::applicationDirPath();

QStringList drivers = QSqlDatabase::drivers();
drivers.removeAll("QMYSQL3");
drivers.removeAll("QOCI8");
drivers.removeAll("QODBC3");
drivers.removeAll("QPSQL7");
drivers.removeAll("QTDS7");

dbDrvComboBox->addItems(drivers);

QObject::connect(dbFileToolButton,
                 SIGNAL(clicked()),
                 this,
                 SLOT(setDatabaseName()));
}

ConnectionDialog::~ConnectionDialog()
{
}

void ConnectionDialog::setDatabaseName()
{
QString filename = QFileDialog::getOpenFileName(this,
                   tr("Select database"),
                   app_dir,
                   tr("All Files (*);;Firebird DB (*.fdb *.ib);;SQLite DB (*.db)"));
    if ( !filename.isEmpty() )
    {
    QString native_filename = QDir::toNativeSeparators(filename);
    dbNameLineEdit->setText(native_filename);
    }
}

QMap<QString, QVariant> ConnectionDialog::return_config()
{
QMap<QString, QVariant> m;

m["driver"] = dbDrvComboBox->currentText();
m["db"]   = dbNameLineEdit->text();

    if (true == dbCredCheckBox->isChecked() )
    {
    m["user"] = dbUsrLineEdit->text();
    m["pass"] = dbPasswdLineEdit->text();
    }
    else
    {
    m["user"] = QLatin1String("");
    m["pass"] = QLatin1String("");
    }

    if ( true == dbHostCheckBox->isChecked() )
    {
    m["host"] = dbHostComboBox->currentText();
    m["port"] = dbPortSpinBox->value();
    }
return m;
}
